﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Input;
using KharaPos.Helpers;
using KharaPos.Web.Entities;
using KharaPos.Web.Services;

namespace KharaPos.ViewModels
{
    public class CatalogManagementViewModel : INotifyPropertyChanged
    {
        private readonly CatalogContext _dataContext;
        private bool _isLoading;

        public CatalogManagementViewModel()
        {
            _catalogCommand = new DelegateCommand(o => AddCatalogToBu());
            _dataContext = new CatalogContext();
            LoadBUs();
        }

        private void LoadBUs()
        {
            IsLoading = true;
            var loadOperation = _dataContext.Load(_dataContext.GetBusinessUnitsQuery());
            loadOperation.Completed += FinishedLoadingBUs;
        }

        protected bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        private readonly ICommand _catalogCommand;
        public ICommand CatalogCommand
        {
            get { return _catalogCommand; }
        }

        private IEnumerable<BusinessUnit> _availableBUs;
        public IEnumerable<BusinessUnit> AvailableBUs
        {
            get { return _availableBUs; }
            set
            {
                _availableBUs = value;
                NotifyPropertyChanged("AvailableBUs");
            }
        }

        private BusinessUnit _selectedBu;
        public BusinessUnit SelectedBu
        {
            get { return _selectedBu; }
            set
            {
                _selectedBu = value;
                SetCurrentBu(_selectedBu);

                NotifyPropertyChanged("SelectedBu");
            }
        }

        private void SetCurrentBu(BusinessUnit selectedBu)
        {
            selectedBu.StoreBusinessUnit();
            var buSubmitOperation = _dataContext.SubmitChanges();
            buSubmitOperation.Completed += BuSubmitOperation_Completed;
        }

        void BuSubmitOperation_Completed(object sender, EventArgs e)
        {
            LoadProducts();
            var operation = (SubmitOperation)sender;
            operation.Completed -= BuSubmitOperation_Completed;

        }

        public void AddCatalogToBu()
        {
            SelectedProduct.CatalogProductForBusinessUnit(SelectedBu.Id);
            _dataContext.SubmitChanges();
        }


        private Product _selectedProduct;
        public Product SelectedProduct
        {
            get { return _selectedProduct; }
            set
            {
                _selectedProduct = value;
                NotifyPropertyChanged("SelectedProduct");
            }
        }

        private void LoadProducts()
        {
            IsLoading = true;
            var productQuery = _dataContext.Load(_dataContext.GetProductsAvailableForCurrentBuQuery(SelectedBu.Id));
            productQuery.Completed += ProductQueryCompleted;
        }

        void ProductQueryCompleted(object sender, EventArgs e)
        {
            var query = (LoadOperation<Product>)sender;
            query.Completed -= ProductQueryCompleted;
            IsLoading = false;
            AvailableProducts = _dataContext.Products;
        }

        private IEnumerable<Product> _availableProducts;

        protected IEnumerable<Product> AvailableProducts
        {
            get { return _availableProducts; }
            set
            {
                _availableProducts = value;
                NotifyPropertyChanged("AvailableProducts");
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        void FinishedLoadingBUs(object sender, EventArgs e)
        {
            AvailableBUs = _dataContext.BusinessUnits;
            IsLoading = false;
            ((LoadOperation<BusinessUnit>)sender).Completed -= FinishedLoadingBUs;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
